package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.upload.adapter.TaskAdapter;
import com.cmcc.linlaker.ui.upload.bean.CheckingTaskBean;
import com.cmcc.linlaker.ui.upload.bean.TaskBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FinishTaskActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_task);

        BarUtils.initStatusBar(this);

        initView();
        getTaskList();
    }

    private TaskAdapter adapter;
    private RecyclerView recycler_finish;
    private List<CheckingTaskBean.CheckingTaskData> itemList = new ArrayList<>();
    private MaterialRefreshLayout refreshLayout;

    public void initView() {
        recycler_finish = findViewById(R.id.recycler_finish);
        setRecyclerView(recycler_finish, itemList);

        refreshLayout = findViewById(R.id.refresh);
        refreshLayout.setLoadMore(false);
        refreshLayout.setWaveShow(true);
        refreshLayout.setWaveColor(Color.parseColor("#3eaefc"));

        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                refreshLayout.finishRefreshLoadMore();
            }
        });
    }

    private void setRecyclerView(RecyclerView recycler_view, final List<CheckingTaskBean.CheckingTaskData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);

        adapter = new TaskAdapter(this, itemList);
        adapter.setOnItemClickListener(new TaskAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(FinishTaskActivity.this, TaskDetailActivity.class);
                intent.putExtra("taskBean", itemList.get(position));
                startActivity(intent);
            }
        });
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void back(View view) {
        finish();
    }

    /**
     * 获取列表
     */
    private void getTaskList() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("status", "2");
        params.put("userId", SPHelper.getUid());
        helper.sendPOSTRequest(ApiDefine.TASK_LIST, CheckingTaskBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    CheckingTaskBean bean = (CheckingTaskBean) data;
                    if (bean.code.equals("0")) {
                        List<CheckingTaskBean.CheckingTaskData> list = bean.rows;
                        if (list != null && list.size() > 0) {
                            itemList.clear();
                            itemList.addAll(list);
                            adapter.notifyDataSetChanged();
                        }else{
                            itemList.clear();
                            adapter.notifyDataSetChanged();
                        }

                        if (itemList.size() > 0) {
                            recycler_finish.setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                        } else {
                            recycler_finish.setVisibility(View.GONE);
                            findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);
                        }
                    }else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(FinishTaskActivity.this);
                    }
                }
            }
        });
    }
}
